package cn.zjyj.module.zjyj.service.riskassessmentrecord;

import cn.hutool.core.collection.CollUtil;
import cn.zjyj.module.zjyj.controller.admin.riskidentifytarget.vo.RiskAssessmentSaveReqVO;
import cn.zjyj.module.zjyj.dal.dataobject.riskapplyrecord.RiskAssessmentApplyRecordDO;
import cn.zjyj.module.zjyj.dal.dataobject.riskidentifyinstance.RiskIdentifyInstanceDO;
import cn.zjyj.module.zjyj.dal.dataobject.riskidentifytarget.RiskIdentifyTargetDO;
import cn.zjyj.module.zjyj.dal.dataobject.riskidentifytargetcontent.RiskIdentifyTargetContentDO;
import cn.zjyj.module.zjyj.dal.mysql.riskapplyrecord.RiskAssessmentApplyRecordMapper;
import cn.zjyj.module.zjyj.dal.mysql.riskidentifyinstance.RiskIdentifyInstanceMapper;
import cn.zjyj.module.zjyj.dal.mysql.riskidentifytarget.RiskIdentifyTargetMapper;
import cn.zjyj.module.zjyj.dal.mysql.riskidentifytargetcontent.RiskIdentifyTargetContentMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.zjyj.module.zjyj.controller.admin.riskassessmentrecord.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskassessmentrecord.RiskAssessmentRecordDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.riskassessmentrecord.RiskAssessmentRecordMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 风险评估记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RiskAssessmentRecordServiceImpl implements RiskAssessmentRecordService {

    @Resource
    private RiskAssessmentRecordMapper riskAssessmentRecordMapper;
    @Resource
    RiskIdentifyInstanceMapper identifyInstanceMapper;
    @Resource
    RiskAssessmentApplyRecordMapper assessmentApplyRecordMapper;
    @Resource
    RiskIdentifyTargetMapper identifyTargetMapper;
    @Resource
    RiskIdentifyTargetContentMapper contentMapper;

    @Override
    public Long createRiskAssessmentRecord(RiskAssessmentRecordSaveReqVO createReqVO) {
        // 插入
        RiskAssessmentRecordDO riskAssessmentRecord = BeanUtils.toBean(createReqVO, RiskAssessmentRecordDO.class);
        riskAssessmentRecordMapper.insert(riskAssessmentRecord);
        // 返回
        return riskAssessmentRecord.getId();
    }

    @Override
    public void updateRiskAssessmentRecord(RiskAssessmentRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateRiskAssessmentRecordExists(updateReqVO.getId());
        // 更新
        RiskAssessmentRecordDO updateObj = BeanUtils.toBean(updateReqVO, RiskAssessmentRecordDO.class);
        riskAssessmentRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskAssessmentRecord(Long id) {
        // 校验存在
        validateRiskAssessmentRecordExists(id);
        // 删除
        riskAssessmentRecordMapper.deleteById(id);
    }

    @Override
    public void deleteRiskAssessmentRecordListByIds(List<Long> ids) {
        // 校验存在
        validateRiskAssessmentRecordExists(ids);
        // 删除
        riskAssessmentRecordMapper.deleteByIds(ids);
    }

    private void validateRiskAssessmentRecordExists(List<Long> ids) {
        List<RiskAssessmentRecordDO> list = riskAssessmentRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RISK_ASSESSMENT_RECORD_NOT_EXISTS);
        }
    }

    private void validateRiskAssessmentRecordExists(Long id) {
        if (riskAssessmentRecordMapper.selectById(id) == null) {
            throw exception(RISK_ASSESSMENT_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RiskAssessmentRecordDO getRiskAssessmentRecord(Long id) {
        return riskAssessmentRecordMapper.selectById(id);
    }

    @Override
    public PageResult<RiskAssessmentRecordDO> getRiskAssessmentRecordPage(RiskAssessmentRecordPageReqVO pageReqVO) {
        return riskAssessmentRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public void submitAssessment(RiskAssessmentSaveReqVO createReqVO) {
        // 修改实体表数据
        RiskIdentifyInstanceDO updDo = RiskIdentifyInstanceDO.builder().id(createReqVO.getIdentifyInstanceId()).status(1).build();
        identifyInstanceMapper.updateById(updDo);
        // 插入记录表
        RiskAssessmentApplyRecordDO riskAssessmentApplyRecordDO = assessmentApplyRecordMapper.selectById(createReqVO.getApplyRecordId());
        Long identifyId = riskAssessmentApplyRecordDO.getIdentifyId();
        createReqVO.getContents().forEach(item -> {
            RiskIdentifyTargetContentDO contentDO = contentMapper.selectById(item.getContentId());
            RiskIdentifyTargetDO targetDO = identifyTargetMapper.selectById(contentDO.getRiskTargetId());

            riskAssessmentRecordMapper.insert(RiskAssessmentRecordDO.builder()
                    .applyRecordId(createReqVO.getApplyRecordId())
                    .identifyId(identifyId)
                    .identifyInstanceId(createReqVO.getIdentifyInstanceId())
                    .riskTargetId(contentDO.getRiskTargetId())
                    .riskTarget(targetDO.getRiskTarget())
                    .riskTargetContentId(item.getContentId())
                    .riskTargetContent(contentDO.getIdentifyContent())
                    .riskDegree(JSONObject.parseObject(JSONObject.toJSONString(item.getRiskDegree())))
                    .riskLevel(item.getRiskLevel())
                    .fixedRiskLevel(item.getFixedRiskLevel())
                    .realRiskLevel(item.getRealRiskLevel())
                    .riskTargetContentUnitId(item.getRiskTargetContentUnitId())
                    .riskTargetContentUnitName(item.getRiskTargetContentUnitId().toString())
                    .riskTargetContentLeaderId(item.getRiskTargetContentLeaderId())
                    .checkCycle(item.getCheckCycle().getNumber())
                    .checkCycleUnit(item.getCheckCycle().getUnit())
                    .checkContents(JSONArray.parseArray(JSONObject.toJSONString(item.getCheckContents())))
                    .build());
        });


    }
}